package com.du.fileload.framework.handler;

import com.du.plugin.common.entity.JResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 异常处理类
 * controller层异常无法捕获处理，需要自己处理
 * Created by jt on 2018/8/27.
 */
@RestControllerAdvice
public class DefaultExceptionHandler implements ResponseBodyAdvice {
    private static final Logger log = LoggerFactory.getLogger(DefaultExceptionHandler.class);
    /**
     * 处理所有自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public JResult handleCustomException(Exception e){
        log.info("发生异常：" +e.getMessage());
        return JResult.newError(e.getMessage());
    }

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        return body;
    }
}